<?php
/*由bean脚本生成 powerBy--joe*/
namespace app\common\manage;

use app\common\bean\CategoryBean;
use app\common\bean\UserBean;
use app\common\enum\CategoryGroupTypeEnum;
use app\common\enum\CategoryTypeEnum;
use app\common\model\CategoryModel;
use app\common\validate\CategoryGroupValidate;
use app\common\model\CategoryGroupModel;
use app\common\bean\CategoryGroupBean;
use app\common\utils\TimeUtil;
use app\common\exception\MyException;
use Exception;

class CategoryGroupManage
{
    static $_self = null;
    private $cgRow = null;//单条组合价格
    public static function getInstance(){
        if(empty(self::$_self)){
            self::$_self = new CategoryGroupManage();
        }
        return self::$_self;
    }
    public function cateCartesian($categoryDatas){
        $store_id = $categoryDatas[0][CategoryBean::$storeId];
        foreach ($categoryDatas as $c){
            $newCateDatas[$c[CategoryGroupBean::$type]][] = $c;
        }
        $cate_arr = CategoryManage::getInstance()->cateArr();
        foreach ($cate_arr as $ck=>$ca){
            $out_arr  = [];
            foreach ($ca as $key=>$value){
                $items_arr = [];
                foreach ($newCateDatas[$key] as $cd){
                    $items_arr[] = $cd[CategoryGroupBean::$id];
                }
                $out_arr[] =  $items_arr;
            }
            $all_com[$ck] = cartesian($out_arr);
        }

        foreach ($all_com as $ak=>$ac){
            foreach ($ac as $a){
                $insert_data[] = [
                    CategoryGroupBean::$storeId=>$store_id,
                    CategoryGroupBean::$categoryIds=>json_encode(explode(',',$a), JSON_NUMERIC_CHECK),
                    CategoryGroupBean::$type=>$ak,
                    CategoryGroupBean::$price=>10,
                    CategoryGroupBean::$vipPrice=>7,
                    CategoryGroupBean::$createdAt=>date('Y-m-d H:i:s')
                ];
            }
        }
        return $insert_data;
    }
    /**
     * 判断会员的单价价格
     * @param int $uid 用户id
     * @param string $cids 分类组合ids
     * @return int 单价
     */
    public function getPrice($uid, $cids){
        $row = $this->singleRow($cids);
        $userBean = new UserBean();
        $userBean->setId($uid);
        UserManage::getInstance()->getInfo($userBean);
        if($userBean->getData()['user_vip']&&$userBean->getData()['user_vip']['is_validity']){
            return $row[CategoryGroupBean::$vipPrice];
        }else{
            return $row[CategoryGroupBean::$price];
        }
    }

    /**
     * 获取组合条记录
     * @param string $cids 分类组合ids
     * @return array 组合条记录
     */
    public function singleRow($cids){
        if($this->cgRow) return $this->cgRow;
        $cidsarr = explode(',', $cids);
        asort($cidsarr);
        $cidsarr = explode(',', implode(',', $cidsarr));
        $strcids = json_encode($cidsarr, JSON_NUMERIC_CHECK);
        $this->cgRow = CategoryGroupModel::getInstance()->selectData([CategoryGroupBean::$categoryIds=>$strcids]);
        return $this->cgRow;
    }
    /**
     * 根据类型，商家获取组合类型
     * @throws Exception
     */
    public function getStoreTypeCategoryGroup(CategoryGroupBean $categoryGroupBean){
        CategoryGroupValidate::getInstance()->goCheck(CategoryGroupValidate::$storeType,$categoryGroupBean->getData());
        $result = CategoryGroupModel::getInstance()->selectData([CategoryGroupBean::$type => $categoryGroupBean->getType(),CategoryGroupBean::$storeId => $categoryGroupBean->getStoreId()],true);
        foreach ($result as &$r){
            $r['category_name'] = CategoryManage::getInstance()->getCateName($r[CategoryGroupBean::$categoryIds]);
        }
        $result !== false ?: MyException::throwMyException("获取组合失败");
        return $result;
    }

	/**
	 * @desc 编辑数据
	 * @param CategoryGroupBean $categoryGroupBean
	 * @return int int
	 * @throws Exception
	**/
	public function edit($categoryGroupBean) {
		//CategoryGroupValidate::getInstance()->goCheck(CategoryGroupValidate::$edit,$categoryGroupBean->getData());
        $filed = CategoryGroupBean::$id;
        $id = $categoryGroupBean->getId();
        $ufiled = $categoryGroupBean->getParameter('field');
        $fvalue = $categoryGroupBean->getParameter('price');
		$result = CategoryGroupModel::getInstance()->updateColumn($filed, $id, $ufiled, $fvalue);
		$result ?: MyException::throwMyException("更新失败");
		return $result;
	}

    /** 处理类型组合
     * @param $type
     * @throws Exception
     */
	public function dealCategoryGroup($type,$storeId){
	    $datas = [];
	    $categoryGroupBean = new CategoryGroupBean();
	    $categoryGroupBean->setStoreId($storeId);
        $categoryGroupBean->setType($type);
	    switch ($type){
            case CategoryGroupTypeEnum::DEVPHOTO://洗照片
                $xzCategory = CategoryModel::getInstance()->selectData([CategoryBean::$storeId => $storeId,CategoryBean::$type => CategoryTypeEnum::DEVPHOTOXZ],true);
                $ccCategory = CategoryModel::getInstance()->selectData([CategoryBean::$storeId => $storeId,CategoryBean::$type => CategoryTypeEnum::DEVPHOTOCC],true);
                foreach ($xzCategory as $key => $value){
                    foreach ($ccCategory as $k => $v){
                        $categoryGroupBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
                        $categoryGroupBean->setCategoryIds(json_encode([$value[CategoryBean::$id],$v[CategoryBean::$id]],JSON_UNESCAPED_UNICODE));
                        $categoryGroupBean->setCategoryNames(json_encode([$value[CategoryBean::$name],$v[CategoryBean::$name]],JSON_UNESCAPED_UNICODE));
                        $categoryGroupBean->setPrice($value[CategoryBean::$price] + $v[CategoryBean::$price]);
                        array_push($datas,$categoryGroupBean->getData());
                    }
                }
                break;
            case CategoryGroupTypeEnum::CERPHOTO://证件照
                $lxCategory = CategoryModel::getInstance()->selectData([CategoryBean::$storeId => $storeId,CategoryBean::$type => CategoryTypeEnum::CERPHOTOLX],true);
                foreach ($lxCategory as $key => $value) {
                    $categoryGroupBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
                    $categoryGroupBean->setCategoryIds(json_encode([$value[CategoryBean::$id]]));
                    $categoryGroupBean->setCategoryNames(json_encode([$value[CategoryBean::$name]],JSON_UNESCAPED_UNICODE));
                    $categoryGroupBean->setPrice($value[CategoryBean::$price]);
                    array_push($datas,$categoryGroupBean->getData());
                }
                break;
            case CategoryGroupTypeEnum::PRIPHOTO://打印
                $scCategory = CategoryModel::getInstance()->selectData([CategoryBean::$storeId => $storeId,CategoryBean::$type => CategoryTypeEnum::PRIPHOTOSC],true);
                $dxCategory = CategoryModel::getInstance()->selectData([CategoryBean::$storeId => $storeId,CategoryBean::$type => CategoryTypeEnum::PRIPHOTODX],true);
                foreach ($scCategory as $key => $value){
                    foreach ($dxCategory as $k => $v){
                        $categoryGroupBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
                        $categoryGroupBean->setCategoryIds(json_encode([$value[CategoryBean::$id],$v[CategoryBean::$id]],JSON_UNESCAPED_UNICODE));
                        $categoryGroupBean->setCategoryNames(json_encode([$value[CategoryBean::$name],$v[CategoryBean::$name]],JSON_UNESCAPED_UNICODE));
                        $categoryGroupBean->setPrice($value[CategoryBean::$price] + $v[CategoryBean::$price]);
                        array_push($datas,$categoryGroupBean->getData());
                    }
                }
                break;
            default:
                MyException::throwMyException("类型出错");
        }
        $result = CategoryGroupModel::getInstance()->deleteData([CategoryBean::$storeId => $storeId,CategoryBean::$type => $type]);
	    $result !== false ?: MyException::throwMyException("删除分类组合失败");
	    CategoryGroupModel::getInstance()->insertAll($datas);
    }

}
